package com.etc.controller;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.etc.entity.Admin;
import com.etc.service.AdminService;
import com.etc.util.MD5Util;
import com.etc.util.Message;
import org.apache.catalina.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 管理员登录后台控制器
 * Created by kl on 2019/5/25.
 */
@Controller
public class AdminController {
    @Autowired
    private AdminService adminService;

    /**
     * 检查数据库中是否存在此用户
     * @param aname
     * @return msg
     */
    @GetMapping(value = "checkName")
    @ResponseBody
    public Message selectAdminById(String aname) {
        Message msg=new Message();
        Admin admin=adminService.getAdminByName(aname);
        if(admin==null){
            msg.setMsg("该用户不存在");
        }
        return msg;
    }
    /**
     * 校验密码是否匹配
     * @param aname
     * @return msg
     */
    @GetMapping(value = "checkPwd")
    @ResponseBody
    public Message checkPwd(String  aname,String apwd,HttpSession session) {
        Message msg=new Message();
        Admin admin=adminService.getAdminByName(aname);
        String apwd1=MD5Util.getEncodeByMd5(apwd);
        String pwd=admin.getApwd();
        if(pwd.equals(apwd1)){
         session.setAttribute("admin",admin);
        }else{
            msg.setMsg("密码错误");
        }
        return msg;
    }


    @GetMapping("loginout")
    public String loginout(HttpSession session) {
        session.invalidate();
        return "redirect:/admin/views/login.jsp";
    }
}
